﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Interface;
using CoVanHocTapData.Entities;

namespace CoVanHocTapData.Concretes
{
    public class QuyDinhLuanVan_TTTNRepository:IQuyDinhLuanVan_TTTNRepository
    {
        DangKyPhuEntities entities;
        public QuyDinhLuanVan_TTTNRepository()
        {
            entities = new DangKyPhuEntities();
        }
        #region Repository
        NganhDaoTaoRepository nganhRepo = new NganhDaoTaoRepository();
        NienKhoaRepository nienKhoaRepo = new NienKhoaRepository();
        HeDaoTaoRepository heDaoTaoRepo = new HeDaoTaoRepository();
        MonHocRepository monHocRepo = new MonHocRepository();
        #endregion

        public IQueryable<QuyDinhLuanVan_TTTN> LayQuyDinhTTTNMoiNhat(string maSV, int soLuong)
        {
            var nganh = nganhRepo.LayNganhTuMaSV(maSV);
            var nienKhoa = nienKhoaRepo.LayNienKhoaTuMaSV(maSV);
            var heDaoTao = heDaoTaoRepo.LayHeDaoTaoTuMaSV(maSV);
            var qd = (from m in entities.QuyDinhLuanVan_TTTN
                      where 
                      m.NganhID.Equals(nganh.NganhID)                      
                      && m.HeDaoTaoID.Equals(heDaoTao.HeDaoTaoID)
                      && m.Loai.Value == 1 // 1: TTTN
                      orderby m.NienKhoaID descending
                      select m).Take(soLuong);
            return qd;
        }

        public IQueryable<QuyDinhLuanVan_TTTN> LayQuyDinhLuanVanMoiNhat(string maSV, int soLuong)
        {
            var nganh = nganhRepo.LayNganhTuMaSV(maSV);
            var nienKhoa = nienKhoaRepo.LayNienKhoaTuMaSV(maSV);
            var heDaoTao = heDaoTaoRepo.LayHeDaoTaoTuMaSV(maSV);
            var qd = (from m in entities.QuyDinhLuanVan_TTTN
                      where
                      m.NganhID.Equals(nganh.NganhID)
                      && m.HeDaoTaoID.Equals(heDaoTao.HeDaoTaoID)
                      && m.Loai.Value == 0 // 1: TTTN
                      orderby m.NienKhoaID descending
                      select m).Take(soLuong);
            return qd;
        }





        public IQueryable<QuyDinhLuanVan_TTTN> LayQuyDinhLV_TTTNTheo(string nganhID, string heDaoTaoID, int nienKhoaID, int loai)
        {
            return from m in entities.QuyDinhLuanVan_TTTN
                   where m.NganhID.Equals(nganhID)
                   && m.HeDaoTaoID.Equals(heDaoTaoID)
                   && m.NienKhoaID.Value == nienKhoaID
                   && m.Loai.Value == loai
                   select m;
        }




        public bool KiemTraQuyDinhLV_TTTNTheo(string nganhID, string heDaoTaoID, int nienKhoaID, int loai)
        {
            var result = LayQuyDinhLV_TTTNTheo(nganhID, heDaoTaoID, nienKhoaID, loai);
            if (result.Count() > 0)
                return true;
            return false;
        }

        public void Update(QuyDinhLuanVan_TTTN qdinh)
        {
            var result = LayQuyDinhLV_TTTNTheo(qdinh.NganhID, qdinh.HeDaoTaoID, qdinh.NienKhoaID.Value, qdinh.Loai.Value).FirstOrDefault();
            result.TenQuyDinh = qdinh.TenQuyDinh;
            result.DTBTLChung = qdinh.DTBTLChung;
            result.SoTinChiTLChung = qdinh.SoTinChiTLChung;
            result.DTBTLKhoiChuyenNganh = qdinh.DTBTLKhoiChuyenNganh;
            result.DTBTLKhoiCoSoKhoiNganhKhoiChuyenNganh = qdinh.DTBTLKhoiCoSoKhoiNganhKhoiChuyenNganh;
            result.DTBTLKhoiCoSoNganh = qdinh.DTBTLKhoiCoSoNganh;
            result.DTBTLKhoiNganh = qdinh.DTBTLKhoiNganh;
            result.CacMonHocKhongDuocNo = qdinh.CacMonHocKhongDuocNo;
            result.SoTinChiToiDaNo = qdinh.SoTinChiToiDaNo;
            result.DiemThucTapTotNghiep = qdinh.DiemThucTapTotNghiep;
            result.DiemTienLuanVan = qdinh.DiemTienLuanVan;
            result.SoLuongSVToiDaDuDKTheoChuyenNganh = qdinh.SoLuongSVToiDaDuDKTheoChuyenNganh;
            result.CacMonHocTruoc = qdinh.CacMonHocTruoc;
            result.CacMonHocTienQuyet = qdinh.CacMonHocTienQuyet;
            entities.SaveChanges();
        }

        public void ThemMoi(QuyDinhLuanVan_TTTN qdinh)
        {
            entities.AddToQuyDinhLuanVan_TTTN(qdinh);
            entities.SaveChanges();
        }
    }
}
